package com.amado.springboot.springbootcms.controll;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.amado.springboot.springbootcms.domain.Depa;
import com.amado.springboot.springbootcms.service.DepaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.amado.springboot.springbootcms.domain.User;
import com.amado.springboot.springbootcms.service.UserService;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2018\2\26 0026.
 */
@Controller
@RequestMapping("/users")
public class DepaController {
	private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
	@Autowired
	private UserService userService;
	@Autowired
	private DepaService depaService;

	@GetMapping
	public String index() {
		LOGGER.debug("进来userArray");
		return "/user/userArray";
	}
	@GetMapping("/Array")
	@ResponseBody
	public List<Depa> list(Map<String, Object> model) {
		LOGGER.debug("返回值" + depaService.getAllDepa());
		return depaService.getAllDepa();
	}
	@PostMapping("/add")
	@ResponseBody
	public Depa add(Depa depa, User user) {
		LOGGER.debug("进来" + depa);
		depaService.addDepa(depa);
		userService.addUser(user);
		LOGGER.debug("返回来" + userService.getAllUser());
		return depa;
	}
	@DeleteMapping("/deleteDepa")
	@ResponseBody
	public String delete(@RequestParam("ids") String ids) {
		LOGGER.debug("zhegeshi{}", ids);
		String[] user = ids.split(",");
		Long[] str2 = new Long[user.length];
		for (int i = 0; i < user.length; i++) {
			str2[i] = Long.valueOf(user[i]);
		}
		depaService.deleteDepa(Arrays.asList(str2));
		return "1";
	}
	@PostMapping("/modifyDepa")
	@ResponseBody
	public void modify(Depa depa) {
		LOGGER.debug("8地址{}" + depa);
		depaService.modifyDepa(depa);
	}
	@GetMapping("/toModifyDepa")
	public String get(@RequestParam("id") Long id, HttpServletRequest request) {
		request.setAttribute("depa", depaService.get(id));
		LOGGER.debug("7地址{}" + depaService.get(id));
		return "dept/depaModify";
	}
	@GetMapping("/pint")
	public String printDate(@RequestParam("id") Long id, HttpServletRequest request) {
		request.setAttribute("depa", depaService.get(id));
		return "Dept/prinDepa";
	}
}
